# -*- coding: utf-8 -*-  
import requests
from lxml import etree
import json
from bug_page import *

def spider(url,staus,i):
	item={}
	
	html=requests.get(url)
	select=etree.HTML(html.text)
	open_time=select.xpath('//div[@class="TableObject-item TableObject-item--primary"]/relative-time/text()')  #获得issue开放时间
	
	if staus=="close":   #获得issue关闭时间
		
		close_time=select.xpath('//div[@class="discussion-item discussion-item-closed"]/div/a[@class="timestamp"]/relative-time/text()')
		link1=select.xpath('//a[@class="commit-link"]/@href')
		
		link2=select.xpath('//div[@class="discussion-item discussion-item-closed"]/div/code/a/@href')
		link=link1 if link1!=[] else link2
	else:
		close_time="at issue"

		link=""

	content_field=select.xpath('//div[@class="timeline-comment-wrapper js-comment-container"]')
	summary=content_field[0].xpath('div[2]/div[@class="edit-comment-hide"]/table/tbody/tr/td/p/text()')
	item["order"]=i
	item["staus"]=staus
	item["open_time"]=open_time
	item["close_time"]=close_time
	item["summary"]=summary
	item["link"]=link
	
	
	#print item.get("link")
	

	#余下评论
	#commend=[]
	#for each in content_field:
		#commend1=each.xpath('div[2]/div[@class="edit-comment-hide"]/table/tbody/tr/td/p/text()')

		#commend.append(commend1[1:])
		
	#item["commend"]=commend
	
	return item
	

	

'''
for i in close_bugnum:
	
	url="https://github.com/composer/satis/issues/{}".format(i)
	spider(url,"close",i)
	

for i in open_bugnum:
url="https://github.com/composer/satis/issues/{}".format(i)	
	spider(url,"open",i)
'''

#url="https://github.com/composer/satis/issues/156"
#spider(url,"open",156)
#url="https://github.com/franzliedke/studio/issues/38"
#spider(url,"close",38)